package com.ctshk.app.user.controller;

import com.ctshk.common.annotations.OperationLogAnnotation;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.IsDeletedCode;
import com.ctshk.common.enums.OperationLogModel;
import com.ctshk.common.enums.OperationLogType;
import com.ctshk.common.model.PageResponse;
import com.ctshk.common.model.TokenUser;
import com.ctshk.rpc.user.dto.es.OrderEsDTO;
import com.ctshk.rpc.user.req.OrderCancelReq;
import com.ctshk.rpc.user.req.OrderPageReq;
import com.ctshk.rpc.user.service.IOrderEsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

/**
 * <p>
 *  C端订单中心前端控制器
 * </p>
 *
 * @author 李新科
 * @date 2021/5/20 17:51
 */
@Api(value="我的订单", tags="我的订单")
@RestController
@RequestMapping("/order")
public class OrderController extends AppBaseController {

    @DubboReference
    private IOrderEsService orderEsService;

    @ApiOperation(value="订单列表", notes="订单列表")
    @OperationLogAnnotation(operModul = OperationLogModel.USER, operType = OperationLogType.QUERY, operTitle = "订单列表", operDesc = "订单列表")
    @PostMapping("/list")
    public Result<PageResponse<OrderEsDTO>> queryPage(@RequestBody @Valid OrderPageReq req) throws Exception{
        TokenUser currentUser = getCurrentUser();
        req.setUserId(currentUser.getId());
        req.setIsDeleted(IsDeletedCode.NO.getCode());
        return orderEsService.queryPage(req);
    }

    @ApiOperation(value="测试-订单数据初始化", notes="测试-订单数据初始化")
    @OperationLogAnnotation(operModul = OperationLogModel.USER, operType = OperationLogType.QUERY, operTitle = "测试-订单数据初始化", operDesc = "测试-订单数据初始化")
    @PostMapping("/init")
    public Result init() throws Exception{
        orderEsService.updateOrderToEs();
        return Result.success();
    }

    @ApiOperation(value="测试-订单更新状态", notes="测试-订单更新状态")
    @OperationLogAnnotation(operModul = OperationLogModel.USER, operType = OperationLogType.UPDATE, operTitle = "测试-订单更新状态", operDesc = "测试-订单更新状态")
    @PostMapping("/update")
    public Result update(Long id,Integer type, Integer orderStatus,Integer paymentStatus, Integer scheduledStatus, Integer cancelStatus,String queryNumber) throws Exception{
        orderEsService.updateStatusToEs(id,type,orderStatus,paymentStatus,scheduledStatus,cancelStatus,queryNumber);
        return Result.success();
    }

    @ApiOperation(value="删除订单", notes="删除订单")
    @OperationLogAnnotation(operModul = OperationLogModel.USER, operType = OperationLogType.DELETE, operTitle = "删除订单", operDesc = "删除订单")
    @PostMapping("/delete/{id}")
    public Result delete(@PathVariable @NotNull(message = "id不能为空")
                         @ApiParam(name="id",required = true) Long id) throws Exception{
        TokenUser currentUser = getCurrentUser();
        return orderEsService.delete(id,currentUser.getId());
    }

    @ApiOperation(value="取消订单", notes="取消订单")
    @OperationLogAnnotation(operModul = OperationLogModel.USER, operType = OperationLogType.DELETE, operTitle = "取消订单", operDesc = "取消订单")
    @PostMapping("/cancel")
    public Result cancel(@RequestBody @Valid OrderCancelReq req) throws Exception{
        req.setTokenUser(getCurrentUser());
        return orderEsService.cancel(req);
    }

    @ApiOperation(value="测试-清空订单", notes="测试-清空订单")
    @OperationLogAnnotation(operModul = OperationLogModel.USER, operType = OperationLogType.DELETE, operTitle = "测试-清空订单", operDesc = "测试-清空订单")
    @PostMapping("/deleteAll")
    public Result deleteAll() throws Exception{
        TokenUser currentUser = getCurrentUser();
        return orderEsService.deleteAll();
    }

}
